/*
 * @Date         : 2024-05-13 17:53:15 星期1
 * @Author       : xut
 * @Description  : 以下消息输出顺序
                    client_a_pre start
                    client_b_pre start
                    client_a_pre sendMessage
                    client_b_pre sendMessage
                    client_a_pre halfClose
                    client_b_pre halfClose
                    client_b_post onReceiveMetadata
                    client_a_post onReceiveMetadata
                    timestamp from header:
                    0. 2024-05-13T11:11:16.009Z
                    Client Trigger Cancel
                    client_a_pre cancel
                    client_b_pre cancel message:
                    client_b_post onReceiveMessage
                    client_a_post onReceiveMessage
                    client_b_post onReceiveStatus
                    client_a_post onReceiveStatus
                    Received unexpected error
                    timestamp expected but doesn't exist in trailer
 */
import grpc from "@grpc/grpc-js"

export const interceptor_a = (options, nextCall) => {
  const client_a_post = new grpc.ListenerBuilder()
    // 接收响应元数据时触发
    .withOnReceiveMetadata((metadata, next) => {
      console.log("client_a_post onReceiveMetadata")
      next(metadata)
    })
    // 接收到响应消息时触发
    .withOnReceiveMessage((message, next) => {
      console.log("client_a_post onReceiveMessage")
      next(message)
    })
    // 接收到状态时触发
    .withOnReceiveStatus((status, next) => {
      console.log("client_a_post onReceiveStatus")
      next(status)
    })
    .build()

  const client_a_pre = new grpc.RequesterBuilder()
    .withStart((metadata, listener, next) => {
      console.log("client_a_pre start")
      next(metadata, client_a_post)
    })
    // 发送到网络通道之前
    .withSendMessage((message, next) => {
      console.log("client_a_pre sendMessage")
      next(message)
    })
    // 在消息发送到网络通道之后调用
    .withHalfClose((next) => {
      console.log("client_a_pre halfClose")
      next()
    })
    // 从客户端取消请求时调用 call.cancel()
    .withCancel((next) => {
      console.log("client_a_pre cancel")
      next()
    })
    .build()

  return new grpc.InterceptingCall(nextCall(options), client_a_pre)
}

export const interceptor_b = (options, nextCall) => {
  const client_b_post = new grpc.ListenerBuilder()
    .withOnReceiveMetadata((metadata, next) => {
      console.log("client_b_post onReceiveMetadata")
      next(metadata)
    })
    .withOnReceiveMessage((message, next) => {
      console.log("client_b_post onReceiveMessage")
      next(message)
    })
    .withOnReceiveStatus((status, next) => {
      console.log("client_b_post onReceiveStatus")
      next(status)
    })
    .build()

  const client_b_pre = new grpc.RequesterBuilder()
    .withStart((metadata, listener, next) => {
      console.log("client_b_pre start")
      next(metadata, client_b_post)
    })
    .withSendMessage((message, next) => {
      console.log("client_b_pre sendMessage")
      next(message)
    })
    .withHalfClose((next) => {
      console.log("client_b_pre halfClose")
      next()
    })
    .withCancel((next) => {
      console.log("client_b_pre cancel message: ")
      next()
    })
    .build()

  return new grpc.InterceptingCall(nextCall(options), client_b_pre)
}
